package 
{
import flash.events.MouseEvent;

import mx.controls.AdvancedDataGrid;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.events.AdvancedDataGridEvent;


/** 
 *  AdvancedDataGrid that only allows editing if you double click
 */
public class DoubleClickAdvancedDataGrid extends AdvancedDataGrid
{

	public function DoubleClickAdvancedDataGrid()
	{
		super();
		doubleClickEnabled = true;
	}

	override protected function mouseDoubleClickHandler(event:MouseEvent):void
	{
       // var AdvancedDataGridEvent:AdvancedDataGridEvent;
        //var ADGE:AdvancedDataGridEvent;
        var adge:AdvancedDataGridEvent;
        var r:IListItemRenderer;
		var dgColumn:AdvancedDataGridColumn;

        r = mouseEventToItemRenderer(event);
		if (r && r != itemEditorInstance)
		{
			var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
			if (columns[dilr.listData.columnIndex].editable)
			{
				dgColumn = columns[dilr.listData.columnIndex];
                adge = new AdvancedDataGridEvent(AdvancedDataGridEvent.ITEM_EDIT_BEGINNING, false, true);
                // ITEM_EDIT events are cancelable
                adge.columnIndex = dilr.listData.columnIndex;
                adge.dataField = dgColumn.dataField;
                adge.rowIndex = dilr.listData.rowIndex + verticalScrollPosition;
                adge.itemRenderer = r;
                dispatchEvent(adge);
            }
        }

		super.mouseDoubleClickHandler(event);
	}

	override protected function mouseUpHandler(event:MouseEvent):void
	{
        var r:IListItemRenderer;
		var dgColumn:AdvancedDataGridColumn;

        r = mouseEventToItemRenderer(event);
		if (r)
		{
			var dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);
			if (columns[dilr.listData.columnIndex].editable)
			{
				dgColumn = columns[dilr.listData.columnIndex];
				dgColumn.editable = false;
			}
		}

		super.mouseUpHandler(event);

		if (dgColumn)
			dgColumn.editable = true;
	}

}

}